<template>
  <div class="app-container">
    <!-- 表格数据 -->
    <el-table :data="data" border style="width: 100%">
      <!-- 序号 -->
      <el-table-column prop="id" label="序号" width="70" align="center">
        <template slot-scope="scope">{{
          scope.$index + (currentPage - 1) * eachPage + 1
        }}</template>
      </el-table-column>
      <!-- 文章名称 -->
      <el-table-column prop="title" label="文章名称" align="center">
        <template slot-scope="scope">
          <el-popover
            placement="top-start"
            title="文章预览图"
            width="200"
            trigger="hover"
          >
            <!-- 图片 -->
            <el-image
              style="width: 200px"
              :src="scope.row.thumb"
              fit="contain"
              :preview-src-list="srcList"
            ></el-image>
            <a
              href="#"
              target="_black"
              slot="reference"
              @click.prevent="goToTitleHandle(scope.row)"
              >{{ scope.row.title }}</a
            >
          </el-popover>
        </template>
      </el-table-column>
      <!-- 文章描述 -->
      <el-table-column prop="address" label="文章描述" align="center">
        <template slot-scope="scope">{{
          scope.row.description
        }}</template></el-table-column
      >
      <!-- 浏览数 -->
      <el-table-column prop="address" label="浏览数" width="130" align="center">
        <template slot-scope="scope">{{
          scope.row.scanNumber
        }}</template></el-table-column
      >
      <!-- 评论量 -->
      <el-table-column prop="address" label="评论量" width="130" align="center">
        <template slot-scope="scope">{{
          scope.row.commentNumber
        }}</template></el-table-column
      >
      <!-- 所属分类 -->
      <el-table-column
        prop="address"
        label="所属分类"
        width="130"
        align="center"
      >
        <template slot-scope="scope">{{
          scope.row.category === null ? "未分类" : scope.row.category.name
        }}</template></el-table-column
      >
      <!-- 创建日期 -->
      <el-table-column
        prop="address"
        label="创建日期"
        width="200"
        align="center"
      >
        <template slot-scope="scope">{{
          scope.row.createDate
        }}</template></el-table-column
      >
      <!-- 操作 -->
      <el-table-column label="操作" align="center" width="130px">
        <template slot-scope="scope">
          <el-tooltip
            class="item"
            effect="dark"
            content="编辑"
            placement="top"
            :hide-after="2000"
          >
            <el-button
              type="primary"
              icon="el-icon-edit"
              circle
              size="mini"
              @click="editBlogHandle(scope.row)"
            ></el-button>
          </el-tooltip>
          <!-- 删除 -->
          <el-tooltip
            class="item"
            effect="dark"
            content="删除"
            placement="top"
            :hide-after="2000"
          >
            <el-button
              type="danger"
              icon="el-icon-delete"
              circle
              size="mini"
              @click="deleteBlog(scope.row)"
            ></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      style="margin-top: 20px"
      small
      :page-size="eachPage"
      :page-sizes="[5, 10, 15, 20]"
      layout="prev, pager, next, total, -> ,sizes, jumper"
      :total="count"
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      @prev-click="prevClickHandle"
      @next-click="nextClickHandle"
    >
    </el-pagination>
  </div>
</template>

<script>
import { getBlogList, deleteBlog } from "@/api/blog";
import { formatDate } from "@/utils/tools.js";
import { server_URL, frontEnd_URL } from "@/urlConfig.js";
export default {
  data() {
    return {
      data: [], //存储文章数据
      srcList: [],
      totalPage: 0, //总页数 默认0
      eachPage: 10, //每页显示条数 默认10
      currentPage: 1, //当前页 默认第一页
      count: 0, //数据的总条数
      pagerCurrentPage: 1, //分页栏当前页码
    };
  },
  created() {
    this.fetchData();
  },
  methods: {
    fetchData() {
      getBlogList(this.currentPage, this.eachPage).then(({ data }) => {
        this.data = data.rows;
        for (var i of this.data) {
          i.createDate = formatDate(i.createDate);
          i.thumb = server_URL + i.thumb;
          this.srcList.push(i.thumb);
        }
        this.count = data.total;
        this.totalPage = Math.ceil(this.count / this.eachPage);
        if (this.currentPage > this.totalPage) {
          this.currentPage = this.totalPage;
          //当前页大于总页数时，当前页等于总页数  出现在 当我这一页剩下一条数据的时候 然后我还把这条数据给删了
          this.fetchData; //重新请求数据
        }
      });
    },
    // 跳转到具体的文章
    goToTitleHandle(blogInfo) {
      window.open(`${frontEnd_URL}/article/${blogInfo.id}`);
    },
    // 编辑文章
    editBlogHandle(blogInfo) {
      this.$router.push({
        path: "/editBlog",
        query: {
          id: blogInfo.id,
        },
      });
    },
    //删除文章
    deleteBlog(blogInfo) {
      this.$confirm(
        "此操作将永久删除该文件删除文章将会一并删除文章评论, 是否继续?",
        "是否删除此篇文章",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          deleteBlog(blogInfo.id).then(() => {
            this.fetchData();
            this.$message({
              type: "success",
              message: "删除成功!",
            });
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // pageSize 改变时会触发
    sizeChangeHandle(eachPage) {
      this.eachPage = parseInt(eachPage);
      this.currentPage = 1;
      this.pagerCurrentPage = 1;
      this.fetchData();
    },
    // currentPage 改变时会触发
    currentChangeHandle(pageNum) {
      this.currentPage = parseInt(pageNum);
      this.fetchData();
    },
    // 用户点击上一页按钮改变当前页后触发
    prevClickHandle() {
      this.currentPage--;
    },
    // 用户点击下一页按钮改变当前页后触发
    nextClickHandle() {
      this.currentPage++;
    },
  },
};
</script>

<style>
</style>